//
//  YTMineSiginController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/8/1.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTMineSiginController.h"
#import "YTSiginButton.h"

#import "YTSignGiftView.h"

@interface YTMineSiginController ()
@property (weak, nonatomic) IBOutlet UIView *siginContentView;
@property (weak, nonatomic) IBOutlet UIView *detailContentView;
@property (weak, nonatomic) IBOutlet UIButton *siginButton; // 签到按钮
@property (weak, nonatomic) IBOutlet UIView *stepContentView;
@property (strong, nonatomic) IBOutletCollection(YTSiginButton) NSArray *siginButtons;
@property (weak, nonatomic) IBOutlet UIView *subSiginContentView;
@property (weak, nonatomic) IBOutlet UILabel *siginLabel;

@property(nonatomic ,strong)StepSlider *slider;

@property(nonatomic ,strong)YTSiginModel *model;

@end

@implementation YTMineSiginController


- (void)setModel:(YTSiginModel *)model
{
    _model = model;
    
    [self.slider setIndex:model.sign.sign_count.intValue - 1];
    
    for (int i = 1; i < model.reward.count; i++) {
        
        YTSiginButton *btn = [self.subSiginContentView viewWithTag:(1000+i)];

        btn.dict = model.reward[i - 1];
    }
    
    for (int i = 0; i < model.sign.sign_count.intValue; i++) {
        YTSiginButton *btn = [self.subSiginContentView viewWithTag:1000 + 1 + i];
        btn.selected = YES;
    }
    
    self.siginLabel.text = [NSString stringWithFormat:@"连续%@天签到",model.sign.sign_count];
}

#pragma mark: - 签到按钮 监听
- (IBAction)siginButtonClick:(UIButton *)sender
{
    if (self.model.sign.sign_count.intValue == 6) {
        YTSignGiftView *view = [YTSignGiftView createViewFromNib];
        view.completionBlock = ^{
            [self loadData];
            
            [YTNetworkManager.shareManager requestUserIsSiginWithCompletion:^(BOOL isSucc, BOOL isSign) {
                if (isSucc) {
                    // 什么都不做
                }
                
                YTNetworkManager.shareManager.account.isSign = [NSNumber numberWithBool:isSign];
            }];
        };
        TYAlertController *alertVc = [TYAlertController alertControllerWithAlertView:view];
        [self presentViewController:alertVc animated:true completion:nil];
        return;
    }
    
    [YTNetworkManager.shareManager requestSiginWithType:@"1" post_id:nil
                                             completion:^(BOOL isSucc, NSString *StatusInfo)
    {
        if (!isSucc) {
            [self.view showTipWithTipStr:StatusInfo];
            return ;
        }
        
        [self.view showAwardWithTipStr:[NSString stringWithFormat:@"签到成功 +%@ETAI",StatusInfo]];
        
        [self loadData];
        
        [YTNetworkManager.shareManager requestUserIsSiginWithCompletion:^(BOOL isSucc, BOOL isSign) {
            if (isSucc) {
                // 什么都不做
            }
            
            YTNetworkManager.shareManager.account.isSign = [NSNumber numberWithBool:isSign];
        }];
    }];
}



// lazy slider
- (StepSlider *)slider
{
    if (!_slider) {
        
        [self.view layoutIfNeeded];
        _slider = [[StepSlider alloc] initWithFrame:self.stepContentView.bounds];
        [_slider setMaxCount:7];
        [_slider setIndex:1];
        _slider.sliderCircleColor = UIColor.clearColor;
        
        [_slider setTrackCircleImage:[UIImage imageNamed:@"yuan_b"] forState:UIControlStateNormal];
        [_slider setTrackCircleImage:[UIImage imageNamed:@"yuan"] forState:UIControlStateSelected];
        _slider.trackHeight = 3;
        
        _slider.tintColor = [UIColor colorWithRed:0.0f/255.0f green:170.0f/255.0f blue:255.0f/255.0f alpha:1];
        _slider.trackColor = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:0.61];
        
        //setShadow
        _slider.layer.masksToBounds = false;
        _slider.layer.shadowColor = [[UIColor colorWithRed:0.0f/255.0f green:18.0f/255.0f blue:18.0f/255.0f alpha:1] CGColor];
        _slider.layer.shadowOffset = CGSizeMake(0, 1);
        _slider.layer.shadowOpacity = 0.2;
        _slider.layer.shadowRadius = 5;
        
        _slider.labels = @[@"1天",@"2天",@"3天",@"4天",@"5天",@"6天",@"7天"];
        _slider.labelFont = [UIFont systemFontOfSize:12];
        //_slider.adjustLabel = YES;
        _slider.labelOffset = 0;

    }
    return _slider;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    [self setupUI];
    
    [self loadData];
}


- (void)loadData
{
    [YTNetworkManager.shareManager requestSiginWithCompletion:^(YTSiginModel *model, BOOL isSucc, NSString *StatusInfo)
    {
        if (!isSucc) {
            [self.view showTipWithTipStr:StatusInfo];
            return ;
        }
        
        self.model = model;
    }];
}

// 设置界面
- (void)setupUI
{
    // setup contentView
    self.siginContentView.layer.masksToBounds = YES;
    self.siginContentView.layer.cornerRadius = 20;
    self.detailContentView.layer.masksToBounds = YES;
    self.detailContentView.layer.cornerRadius = 20;
    
    //setShadow
    self.siginContentView.layer.masksToBounds = false;
    self.siginContentView.layer.shadowColor = [[UIColor colorWithRed:0.0f/255.0f green:227.0f/255.0f blue:225.0f/255.0f alpha:1] CGColor];
    self.siginContentView.layer.shadowOffset = CGSizeMake(0, 1);
    self.siginContentView.layer.shadowOpacity = 0.1;
    self.siginContentView.layer.shadowRadius = 5;
    
    self.detailContentView.layer.masksToBounds = false;
    self.detailContentView.layer.shadowColor = [[UIColor colorWithRed:0.0f/255.0f green:227.0f/255.0f blue:225.0f/255.0f alpha:1] CGColor];
    self.detailContentView.layer.shadowOffset = CGSizeMake(0, 1);
    self.detailContentView.layer.shadowOpacity = 0.1;
    self.detailContentView.layer.shadowRadius = 5;
    
    // setup navbar
    [self.navigationController.navigationBar  setTitleTextAttributes:@{
                                                                       NSForegroundColorAttributeName:[UIColor whiteColor],
                                                                       NSFontAttributeName : [UIFont systemFontOfSize:15]
                                                                       }];
    
   
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fanhui"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self action:@selector(backButtonClick)];
    
    [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    
    [self.stepContentView addSubview:self.slider];

    
    
}
- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    
    
    
    self.stepContentView.userInteractionEnabled = false;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self showNavigationWithClearBGWIthRect:CGRectMake(0, 0, 1, 1) alpha:0];
}


#pragma mark: - 返回按钮 监听
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:true];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
   [self showNavigationWithClearBGWIthRect:self.navigationController.navigationBar.bounds alpha:1];
}



- (void)showNavigationWithClearBGWIthRect:(CGRect)rect alpha:(CGFloat)alpha{

    self.navigationController.navigationBar.translucent = (rect.size.width > 1) ? NO : YES;

    // 将状态栏和导航条设置成透明
    UIImage *image = [UIImage imageWithColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:alpha] rect:rect];


    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}





@end
